package integraltask

import (
	"context"

	"mall/common/globalkey"
	"mall/common/jwt"
	"mall/common/resultx"
	"mall/service/backend/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/setting/rpc/settingclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetIntegralTaskByIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetIntegralTaskByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetIntegralTaskByIdLogic {
	return &GetIntegralTaskByIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetIntegralTaskByIdLogic) GetIntegralTaskById(req *types.IDReq) (*types.IntegralTaskInfo, error) {
	logx.Infof("GetIntegralTaskById : %+v ", req)
	if req.Id == 0 {
		return nil, resultx.NewErrCode(resultx.FORUM_POST_ID_REQUIRED)
	}

	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}

	var preload string = "integralTask:Id,RuleName,RuleSlug,RuleType,PicId,PicUrl"
	preload += ",Frequency,Points,EffectiveTime,Sort,Description,Status"
	var eagerLoad string = "integralTask:"
	var platformId uint32 = globalkey.ThirdPartyPlatformWechatMini

	res, err := l.svcCtx.SettingRpc.GetIntegralTaskById(l.ctx, &settingclient.IntegralTaskDetailReq{
		Id:         pointy.GetPointer(req.Id),
		Preload:    pointy.GetPointer(preload),
		EagerLoad:  pointy.GetPointer(eagerLoad),
		VisitorId:  pointy.GetPointer(userId),
		PlatformId: pointy.GetPointer(platformId),
	})
	if err != nil {
		return nil, err
	}

	return &types.IntegralTaskInfo{
		BaseIDInfo: types.BaseIDInfo{
			Id:        res.Id,
			CreatedAt: res.CreatedAt,
			UpdatedAt: res.UpdatedAt,
			DeletedAt: res.DeletedAt,
		},
		RuleName:      res.RuleName,
		RuleSlug:      res.RuleSlug,
		RuleType:      res.RuleType,
		PicId:         res.PicId,
		PicUrl:        res.PicUrl,
		Frequency:     res.Frequency,
		Points:        res.Points,
		EffectiveTime: res.EffectiveTime,
		Description:   res.Description,
		Sort:          res.Sort,
		Status:        res.Status,
	}, nil
}
